package business.service.user.impl;

import java.io.IOException;

import business.entity.User;
import business.service.user.IUserRegistService;

import app.MyApplication;

import ui.model.user.UserRegisterViewModel;
import util.web.WebServiceHelper;

/*
 *Author cxs
 *Date 2013-05-27
 */
public class UserRegistService implements IUserRegistService {
	public UserRegistService(UserRegisterViewModel model) {
		this.model = model;
		url = MyApplication.url + "/register";
	}

	public UserRegisterViewModel getModel() {
		return model;
	}

	public void setModel(UserRegisterViewModel model) {
		this.model = model;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	private String url = null;
	private UserRegisterViewModel model = null;

	@Override
	public boolean register(User user) {
		model.setUser(user);
		if (model.verifyModel() == null) {
			// TODO 这里下次迭代再改成动态的
			String msg = "username=" + user.getName() + "&password=" + user.getPasswd();
			try {
				String result = WebServiceHelper.post(url, msg);
				if (result.equals("true")) {
					return true;
				} else {
					// TODO 用户已经注册
					return false;
				}
			} catch (IOException e) {
				// TODO 网络有問題
				return false;
			}
		}
		model.fireOnUpdated();
		return false;
	}

}
